<?php require_once('Connections/RecursosHumanos.php'); ?>
<?php
$currentPage = $_SERVER["PHP_SELF"];

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$UsuarioActivo = $_GET['ID_INGRESO'];

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO departamento (ID_DEPARTAMENTO, DES_DEPTO) VALUES (%s, %s)",
                       GetSQLValueString($_POST['ID_DEPARTAMENTO'], "text"),
                       GetSQLValueString($_POST['DES_DEPTO'], "text"));

  mysql_select_db($database_RecursosHumanos, $RecursosHumanos);
  $Result1 = mysql_query($insertSQL, $RecursosHumanos) or die(mysql_error());

  $insertGoTo = "CrearDepto.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

$maxRows_Deptos = 10;
$pageNum_Deptos = 0;
if (isset($_GET['pageNum_Deptos'])) {
  $pageNum_Deptos = $_GET['pageNum_Deptos'];
}
$startRow_Deptos = $pageNum_Deptos * $maxRows_Deptos;

mysql_select_db($database_RecursosHumanos, $RecursosHumanos);
$query_Deptos = "SELECT * FROM departamento ORDER BY departamento.ID_DEPARTAMENTO";
$query_limit_Deptos = sprintf("%s LIMIT %d, %d", $query_Deptos, $startRow_Deptos, $maxRows_Deptos);
$Deptos = mysql_query($query_limit_Deptos, $RecursosHumanos) or die(mysql_error());
$row_Deptos = mysql_fetch_assoc($Deptos);

if (isset($_GET['totalRows_Deptos'])) {
  $totalRows_Deptos = $_GET['totalRows_Deptos'];
} else {
  $all_Deptos = mysql_query($query_Deptos);
  $totalRows_Deptos = mysql_num_rows($all_Deptos);
}
$totalPages_Deptos = ceil($totalRows_Deptos/$maxRows_Deptos)-1;

$queryString_Deptos = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_Deptos") == false && 
        stristr($param, "totalRows_Deptos") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_Deptos = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_Deptos = sprintf("&totalRows_Deptos=%d%s", $totalRows_Deptos, $queryString_Deptos);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento sin t&iacute;tulo</title>
<style type="text/css">
<!--
body {
	margin-top: 1cm;
	margin-left: 2cm;
}
.Estilo1 {font-size: 12px}
-->
</style></head>

<body>

<?php echo "Bienvenido: ".$UsuarioActivo;?>

<form method="post" name="form1" action="<?php echo $editFormAction;?>">
  <p><a href="home.html">REGRESAR</a></p>
  <table align="left">
    <tr valign="baseline">
      <td nowrap align="right">Id departamento :</td>
      <td><input name="ID_DEPARTAMENTO" type="text" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left">Descripcion:</div></td>
      <td><input type="text" name="DES_DEPTO" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">&nbsp;</td>
      <td><input type="submit" value="Insertar registro"></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1">
</form>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<table border="1" align="left">
  <tr>
    <td width="175">Id departamento </td>
    <td width="222">Descripcion</td>
  </tr>
  <?php do { ?>
    <tr>
      <td><span class="Estilo1"><?php echo $row_Deptos['ID_DEPARTAMENTO']; ?></span></td>
      <td><span class="Estilo1"><a href="ModificarDepartamento.php?ID_DEPTO_MOD=<?php echo $row_Deptos['ID_DEPARTAMENTO']; ?>"><?php echo $row_Deptos['DES_DEPTO']; ?></a></span></td>
    </tr>
    <?php } while ($row_Deptos = mysql_fetch_assoc($Deptos)); ?>
</table>

<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<table border="0" width="50%" align="left">
  <tr>
    <td width="23%" align="center"><?php if ($pageNum_Deptos > 0) { // Show if not first page ?>
          <a href="<?php printf("%s?pageNum_Deptos=%d%s", $currentPage, 0, $queryString_Deptos); ?>">Primero</a>
          <?php } // Show if not first page ?>
    </td>
    <td width="31%" align="center"><?php if ($pageNum_Deptos > 0) { // Show if not first page ?>
          <a href="<?php printf("%s?pageNum_Deptos=%d%s", $currentPage, max(0, $pageNum_Deptos - 1), $queryString_Deptos); ?>">Anterior</a>
          <?php } // Show if not first page ?>
    </td>
    <td width="23%" align="center"><?php if ($pageNum_Deptos < $totalPages_Deptos) { // Show if not last page ?>
          <a href="<?php printf("%s?pageNum_Deptos=%d%s", $currentPage, min($totalPages_Deptos, $pageNum_Deptos + 1), $queryString_Deptos); ?>">Siguiente</a>
          <?php } // Show if not last page ?>
    </td>
    <td width="23%" align="center"><?php if ($pageNum_Deptos < $totalPages_Deptos) { // Show if not last page ?>
          <a href="<?php printf("%s?pageNum_Deptos=%d%s", $currentPage, $totalPages_Deptos, $queryString_Deptos); ?>">&Uacute;ltimo</a>
          <?php } // Show if not last page ?>
    </td>
  </tr>
</table>



</body>
</html>
<?php
mysql_free_result($Deptos);
?>
